<template>
    <div class="Agent-info-manage" v-loading="loading" element-loading-text="加载中。。。">
        <div>
            <el-card class='cloud-teller-card'>
                <el-form ref='queryForm' :model='queryForm' size='mini' label-position='top'>
                    <el-row :gutter="10">
                        <el-col :md="6" :lg="4">
                            <el-form-item label='中心编号' class='typeInfo-el-form-item' prop='CENTERID'>
                                <el-input v-model='queryForm.CENTERID' placeholder="请输入中心编号"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :md="6" :lg="4">
                            <el-form-item label='机构号' class='typeInfo-el-form-item' prop='BRANCHNO'>
                                <el-input v-model='queryForm.BRANCHNO' placeholder="请输入机构号"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :md="3" :lg="2" class="searchBtnMarginTop">
                            <el-button type='primary' size='mini' icon='el-icon-search' @click="queryPage">查询</el-button>
                        </el-col>
                        <el-col :md="3" :lg="2" class="searchBtnMarginTop">
                            <el-button type='warning' size='mini' icon='el-icon-refresh' @click="clearForm('queryForm')">重置</el-button>
                        </el-col>
                    </el-row>
                </el-form>
            </el-card>
            <el-card style="margin-top: 10px">
                <el-col :lg="2" :md="2">
                    <el-button type="primary" size="mini" @click="handleClickDialog('add')" icon='el-icon-plus'>新增</el-button>
                </el-col>
                <el-col :lg="2" :md="2">
                    <el-button type="primary" size="mini" @click="handleClickDialog('update')" icon='el-icon-edit' :disabled="this.currentRowData.length==1?false:true">修改</el-button>
                </el-col>
                <el-col :lg="2" :md="2">
                    <el-button type="danger" size="mini" @click="deleteConfirm" icon='el-icon-delete' :disabled="this.currentRowData.length==1?false:true">删除</el-button>
                </el-col>
                <el-table ref="tableData" :data='tableDataPage' border min-height="400" width='100%' class="tableMarginTop" header-row-class-name="tableHeaderClass" highlight-current-row @selection-change="selectChange">
                    <el-table-column type="selection" width="40px"></el-table-column>
                    <el-table-column prop='CENTERID' label='中心编号'></el-table-column>
                    <el-table-column prop='CENTERNAME' label='中心名称'></el-table-column>
                    <el-table-column prop='BRANCHNO' label='机构编号'></el-table-column>
                    <el-table-column prop='BRANCHNONAME' label='机构名称'></el-table-column>
                    <el-table-column prop='USEFLAG' label='状态'>
                        <template slot-scope="scope">
                      <span v-if="scope.row.USEFLAG=='0'">启用</span>
                      <span v-if="scope.row.USEFLAG=='1'">不启用</span>
</template>
                </el-table-column>
            </el-table>
            <!-- 翻页按钮 -->
            <el-pagination style="text-align:right;margin-top:20px;" 
                background 
                v-show="this.tableData" 
                :current-page="currentPage" 
                :page-size="pageSize"
                @current-change="changePage" 
                layout="total, prev, pager, next, jumper" 
                :total="total">
            </el-pagination>
            
            <!-- 弹窗 -->
            <keep-alive>
                <component :is='dialogList.addDialog' :option='addOption' @refreshTable="queryPage"></component>
            </keep-alive>
            <keep-alive>
                <component :is='dialogList.updateDialog' :option='updateOption' @refreshTable="queryPage" :message="this.currentRowData"></component>
            </keep-alive>

        </el-card>
    </div>

  </div>
</template>

<script>
    import getGlobalParams from '@/common/utils/getGlobalParams';
    //通讯相关
    import request from "@/common/utils/requestCT";
    import queryBranch from "@/message/CTManage/teller/cloud-teller-branch-query";
    import delInfo from "@/message/CTManage/branch-center/cloud-teller-branch-del";
    // 弹窗
    import addDialog from "./dialog/add";
    import updateDialog from "./dialog/update"
    export default {
        name: "Agent-info-manage",
        components: {},
        data() {
            return {
                loading: false,
                pageSize: getGlobalParams.get('PageSize'),
                currentPage: 1,
                total: 0,
                currentRowData: [],
                seletedPostid: [],
                dialogList: {
                    addDialog,
                    updateDialog
                },
                addOption: {
                    isShow: false
                },
                updateOption: {
                    isShow: false
                },
                queryForm: {
                    CENTERID: '',
                    BRANCHNO: '',
                    USEFLAG: ''
                },
                tableData: [],
            };
        },
        mounted() {
            this.queryModel();
        },
        computed: {
            tableDataPage() {
                return this.tableData.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize);
            }
        },
        methods: {
            queryPage() {
                this.currentPage = 1;
                this.queryModel();
            },
            queryModel() {
                this.currentRowData = [];
                this.loading = true;
                let resBody = new queryBranch();
                resBody.data.CENTERID = this.queryForm.CENTERID;
                resBody.data.BRANCHNO = this.queryForm.BRANCHNO;
                resBody.QueryPageNo = this.currentPage;
                resBody.QueryRowNum = this.pageSize;
                request(resBody)
                    .then(response => {
                        if (response.SYS_HEAD.ReturnCode == "000000") {
                            this.tableData = response.RSP_BODY.resultSet;
                            this.total = this.tableData.length;
                        } else {
                            this.$message({
                                type: "error",
                                message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
                            });
                        }
                        this.loading = false;
                    })
                    .catch(error => {
                        this.$message({
                            type: "warning",
                            message: "页面出错！"
                        });
                        this.loading = false;
                        console.error(error)
                    })
            },
            // 点击新增修改按钮事件
            handleClickDialog(val) {
                if (val === 'add') {
                    this.addOption.isShow = true;
                } else if (val === 'update') {
                    this.updateOption.isShow = true;
                } else {
                    console.error("error")
                }
            },
            deleteConfirm() {
                this.$confirm("此操作将永久删除信息，是否继续？", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning"
                    })
                    .then(() => {
                        this.delCloudTeller();
                    })
                    .catch(() => {
                        this.$message({
                            type: "info",
                            message: "已取消删除"
                        });
                    });
            },
            delCloudTeller() {
                this.loading = true;
                let resBody = new delInfo();
                resBody.data.CENTERID = this.currentRowData[0].CENTERID;
                resBody.data.BRANCHNO = this.currentRowData[0].BRANCHNO;
                request(resBody)
                    .then(response => {
                        if (response.SYS_HEAD.ReturnCode == "000000") {
                            this.$message({
                                type: "success",
                                message: "删除成功!"
                            });
                            this.queryPage()
                        } else {
                            this.$message({
                                type: "error",
                                message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
                            });
                        }
                        this.loading = false;
                    })
                    .catch(error => {
                        this.$message({
                            type: "warning",
                            message: "页面出错!"
                        });
                        this.loading = false;
                        console.error(error);
                    })
            },
            selectChange(val) {
                this.currentRowData = val
                this.seletedPostid = [];
                for (let i in val) {
                    this.seletedPostid[i] = val[i].CENTERID;
                }
            },
            changePage(val) {
                this.currentPage = val
            },
            clearForm(item) {
                this.$refs["" + item].resetFields();
                this.queryModel();
            },
        },
    };
</script>


<style lang="scss" scoped>

</style>
